package com.example.myproject;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.example.bean.User;

import java.lang.reflect.Field;

public class MainActivity extends AppCompatActivity {

    private TextView main_content;
    private User user1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        main_content = findViewById(R.id.main_content);

        User user = new User("张三", 20, "男");//实际显示  张三

        user1 = new User("找六", 21, "男男");//实际显示  找六

//        changeUser(user1);// 未能修改 user1 的值

//        changeUserSet(user1);//修改了  user1 内部的值
        changeUserFanShe(user1);
        main_content.setText(user1.getName());
    }

    public void goNext(View v) {
        //  问题，什么是序列化？如何做到跨平台？转为什么样的通用格式
        Intent intent = new Intent(this, SecondActivity.class);
        User user = new User("李梅梅", 18, "女");
        Bundle bundle = new Bundle();
        bundle.putParcelable("test", user);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    private void changeUserFanShe(Object ob) {
        Class<?> aClass = ob.getClass();
        Field[] fields = aClass.getDeclaredFields();
        for (Field fie : fields) {
            fie.setAccessible(true);
            try {
                if (fie.getName().equals("name"))
                    fie.set(ob, "京八");
                else if (fie.getName().equals("age"))
                    fie.set(ob, 33);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }

    }

    private void changeUserSet(User ua) {
        ua.setName("王五");
        ua.setAge(17);
        ua.setSex("女");
    }


    private void changeUser(User ua) {
        ua = new User("王五", 10, "男");
    }


}
